package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.Video;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import javax.swing.text.AbstractDocument;
import java.util.Date;
import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //分页查询
    public PageResult findAll(Integer page, Integer pageSize, String publishId) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId)));
        query.with(PageRequest.of(page-1,pageSize, Sort.by(Sort.Direction.DESC,"created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pageSize,(int)count,commentList);
    }

    //分页查询
    public PageResult findAll(Integer page, Integer pageSize, String publishId,Integer commentType) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(commentType));
        query.with(PageRequest.of(page-1,pageSize, Sort.by(Sort.Direction.DESC,"created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pageSize,(int)count,commentList);
    }

    @Override
    public PageResult findAllVideosComments(Integer page, Integer pagesize, String publishId) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId)));
        query.with(PageRequest.of(page-1,pagesize, Sort.by(Sort.Direction.DESC,"created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,commentList);
    }

    @Override   //返回的是评论完后的数量
    public Integer save(Comment comment) {
//        这个评论是可以对圈子、小视频、评论
        comment.setId(new ObjectId());
        comment.setCreated(new Date().getTime());
        ObjectId publishId = comment.getPublishId(); //这个ID有可能是圈子的id也可以是小视频的id也可以是评论的id
//        comment.getPubType();  //评论内容类型： 1-对动态操作 2-对视频操作 3-对评论操作
        if(comment.getPubType()==1){  //对圈子评论的操作 这个评论包括：点赞、喜欢、评论

//            1、对应的这个圈子数据上的点赞数+1  update publish set likeCount=likeCount+1 where id=???
            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),1);
            //评论类型，1-点赞，2-评论，3-喜欢
            mongoTemplate.updateFirst(query,update,Publish.class);


            Publish publish = mongoTemplate.findById(publishId, Publish.class);
            comment.setPublishUserId(publish.getUserId()); //是谁发布的这个圈子动态
//            2、保存评论对象
            mongoTemplate.save(comment);
            publish.getLikeCount();publish.getLoveCount();publish.getCommentCount();
            return comment.getCount(publish);
        }
        if(comment.getPubType()==2){
//          TODO  2-对小视频操作
            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc("likeCount",1);
            //评论类型，1-点赞，2-评论，3-喜欢
            mongoTemplate.updateFirst(query,update, Video.class);


            Video video = mongoTemplate.findById(publishId, Video.class);
            comment.setPublishUserId(video.getUserId()); //是谁发布的这个圈子动态
//            2、保存评论对象
            mongoTemplate.save(comment);
            return video.getCommentCount();
        }
        if(comment.getPubType()==3){
//        TODO     3-对发过的评论操作

 //            1、对应的这个评论数据上的点赞数+1  update comment set likeCount=likeCount+1 where id=???
            Query query = new Query(Criteria.where("id").is(publishId));  // 这个publishId里面放的是评论的id
            Update update = new Update();
            update.inc("likeCount",1);
            //评论类型，1-点赞，2-评论，3-喜欢
            mongoTemplate.updateFirst(query,update,Comment.class);


            Comment commentById = mongoTemplate.findById(publishId, Comment.class);
            comment.setPublishUserId(commentById.getUserId()); //是谁发布的这个圈子动态
//            2、保存评论对象
            mongoTemplate.save(comment);

            return commentById.getLikeCount();  //获取的是这个评论上的点赞数

        }



		return 0;
    }

    @Override //返回这个圈子或小视频或评论的 的点赞数|喜欢数|评论数
    public Integer remove(Comment comment) {
        //        这个评论是可以对圈子、小视频、评论
        ObjectId publishId = comment.getPublishId(); //这个ID有可能是圈子的id也可以是小视频的id也可以是评论的id

//        删除评论数据
        Query query = new Query(Criteria.
                where("publishId").is(publishId)
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId()));
        mongoTemplate.remove(query,Comment.class);

        if(comment.getPubType()==1){  //对圈子评论的操作 这个评论包括：点赞、喜欢、评论

            query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),-1);
            //评论类型，1-点赞，2-评论，3-喜欢
            mongoTemplate.updateFirst(query,update,Publish.class);
            Publish publish = mongoTemplate.findById(publishId, Publish.class);
            return comment.getCount(publish);

        }
        if(comment.getPubType()==2){
//          TODO  2-对小视频操作
            query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),1);
            //评论类型，1-点赞，2-评论，3-喜欢
            mongoTemplate.updateFirst(query,update, Video.class);
            Video video = mongoTemplate.findById(publishId, Video.class);
            return video.getCommentCount();
        }
        if(comment.getPubType()==3){
//        TODO     3-对发过的评论操作
            query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),-1);
            //评论类型，1-点赞，2-评论，3-喜欢
            mongoTemplate.updateFirst(query,update,Comment.class);
            Comment comment1 = mongoTemplate.findById(publishId, Comment.class);
            return comment1.getLikeCount();
        }

        return 0;
    }

    /**
     * db.quanzi_comment.find({publishUserId:1,commentType:1})
     * db.quanzi_comment.find({publishUserId:1,commentType:2})
     * db.quanzi_comment.find({publishUserId:1,commentType:3})
     */
    @Override
    public PageResult findByPublishUserId(Long publishUserId, int commentType, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType));
        query.with(PageRequest.of(page-1,pagesize,Sort.by(Sort.Direction.DESC,"created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
       long count = mongoTemplate.count(query, Comment.class);

        return new PageResult(page,pagesize,(int)count,commentList);
    }
}